<?php
/**
 * Celebrio FileSystem module
 *
 * @copyright  Copyright (c) 2011 Celebrio Software
 * @package    Celebrio
 * @subpackage FileSystem
 */
namespace Celebrio\FileSystem;

use Nette\Object;

use InvalidArgumentException;

use Celebrio\FileSystem\IFile;



/**
 * Generic File object containing information which every
 * FS file object must have: URL and name.
 *
 * Simple value-checking methods are implemented in the
 * setters so that every virtual file structure (now it's
 * only DataFile and VirtualFile) should inherit this class.
 *
 * @author pavel
 *
 * @todo handle exceptions
 */
abstract class GenericFile extends Object implements IFile {
    
    const NONE = 0;
    
    const BASIC_INFO = 1;
    const OWNER = 2;
    const LOCKEDBY = 4;
    const INFO = 7;
    
    const FSTORAGE = 8;
    const MIMETYPE = 16;
    const STORMIM = 24;
    
    const ALL = 31;
    
    /** @var String */
    protected $url = null;
    
    /** @var String */
    protected $name = null;
    
    /**
     *
     * @param String $name
     * @param String $url
     *
     * @throws InvalidArgumentException
     */
    public function  __construct($name, $url) {
        $this->setName($name); //setName() must be called first
        $this->setUrl($url);
    }


    /**
     * Checks whether the name is not empty ("", null or some simmilar
     * kind of shit) and sets the name attribute.
     * If name is null and url is "root/" the name is set as root and 
     * root as null because of root directory is calling on VF.
     * If name is null and url is "/" name is set as null or ""
     * (empty at least).
     *
     * @param String $name
     *
     * @throws InvalidArgumentException
     */
    public function setName($name) {
        if (empty($name)) {	
            throw new InvalidArgumentException("File name must not be empty!");
        }
        $this->name = $name;        
    }

    /**
     * If the last character of the URL is not '/', it's added
     * to the end of the URL and then set. Also, URL must not
     * be null.
     *
     * @param string $url
     *
     * @throws InvalidArgumentException
     */
    public function setUrl($url) {
        if (empty($url)) {
            throw new InvalidArgumentException("URL must not be empty (null, \"\", etc...).");
        }
        if (substr($url, -1, 1) != '/') {
            $url = $url . '/';
        }
        $this->url = $url;
    }
    
    public function getName() {
        return $this->name;
    }

    public function getUrl() {
        return $this->url;
    }
}
